
<template>
  <view class="uni-container">
    <uni-forms ref="form" :value="formData" validateTrigger="bind">
      <uni-forms-item name="openid" label="用户openid">
  <uni-easyinput placeholder="用户id" v-model="formData.openid" disabled="true" />
</uni-forms-item>
<uni-forms-item name="hotelName" label="民宿名称">
  <uni-easyinput placeholder="民宿名称" v-model="formData.hotelName" trim="both" disabled="true" />
</uni-forms-item>
<uni-forms-item name="name" label="入住人名称">
  <uni-easyinput placeholder="入住人名称" v-model="formData.name" />
</uni-forms-item>
<uni-forms-item name="phone" label="手机号码">
  <uni-easyinput placeholder="手机号码" v-model="formData.phone" />
</uni-forms-item>
<uni-forms-item name="arriveTime" label="到达时间">
	<uni-datetime-picker type="time" return-type="string" :value="formData.arriveTime"/>
</uni-forms-item>
<uni-forms-item name="remark" label="备注">
  <uni-easyinput placeholder="备注" v-model="formData.remark" />
</uni-forms-item>
<uni-forms-item name="startDate" label="入住时间">
	<uni-datetime-picker type="date" return-type="string" :value="formData.startDate" disabled="true" />
</uni-forms-item>
<uni-forms-item name="endDate" label="退房时间">
  <uni-datetime-picker type="date" return-type="string" :value="formData.endDate" disabled="true" />
</uni-forms-item>
<uni-forms-item name="status" label="订单状态">
  <uni-data-checkbox v-model="formData.status" :localdata="formOptions.status_localdata" />
</uni-forms-item>

      <view class="uni-button-group">
        <button type="primary" class="uni-button" style="width: 100px;" @click="submit">提交</button>
        <navigator open-type="navigateBack" style="margin-left: 15px;">
            <button class="uni-button" style="width: 100px;">返回</button>
        </navigator>
      </view>
    </uni-forms>
  </view>
</template>

<script>
  import { validator } from '../../js_sdk/validator/order.js';

  const db = uniCloud.database();
  const dbCmd = db.command;
  const dbCollectionName = 'order';

  function getValidator(fields) {
    let reuslt = {}
    for (let key in validator) {
      if (fields.includes(key)) {
        reuslt[key] = validator[key]
      }
    }
    return reuslt
  }

  export default {
    data() {
      return {
        formData: {
  "openid": "",
  "hotel_id": "",
  "hotelName": "",
  "name": "",
  "phone": "",
  "arriveTime": "",
  "remark": "",
  "startDate": "",
  "endDate": "",
  "status": 0,
  "create_date": null
},
	formOptions: {
		"status_localdata": [
			{
				"text": "待入住",
				"value": 0
			},
			{
				"text": "已入住",
				"value": 1
			},
			{
				"text": "已取消",
				"value": 2
			},
			{
				"text": "已删除",
				"value": 3
			}
		]
	},
        rules: {
          ...getValidator(["openid","hotel_id","hotelName","name","phone","arriveTime","remark","startDate","endDate","status","create_date"])
        }
      }
    },
    onLoad(e) {
      const id = e.id
      this.formDataId = id
      this.getDetail(id)
    },
    onReady() {
      this.$refs.form.setRules(this.rules)
    },
    methods: {
      /**
       * 触发表单提交
       */
      submit() {
        uni.showLoading({
          mask: true
        })
        this.$refs.form.submit().then((res) => {
          this.submitForm(res)
        }).catch((errors) => {
          uni.hideLoading()
        })
      },

      submitForm(value) {
        // 使用 clientDB 提交数据
        db.collection(dbCollectionName).doc(this.formDataId).update(value).then((res) => {
          uni.showToast({
            title: '修改成功'
          })
          this.getOpenerEventChannel().emit('refreshData')
          setTimeout(() => uni.navigateBack(), 500)
        }).catch((err) => {
          uni.showModal({
            content: err.message || '请求服务失败',
            showCancel: false
          })
        }).finally(() => {
          uni.hideLoading()
        })
      },

      /**
       * 获取表单数据
       * @param {Object} id
       */
      getDetail(id) {
        uni.showLoading({
          mask: true
        })
        db.collection(dbCollectionName).doc(id).field('openid,hotel_id,hotelName,name,phone,arriveTime,remark,startDate,endDate,status,create_date').get().then((res) => {
          const data = res.result.data[0]
          if (data) {
            this.formData = data
          }
        }).catch((err) => {
          uni.showModal({
            content: err.message || '请求服务失败',
            showCancel: false
          })
        }).finally(() => {
          uni.hideLoading()
        })
      }
    }
  }
</script>

